#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    static const int N = 10010;
    struct Node
    {
        int c, r, h;
    } sum[N];

    bool check(int i, vector<int> re)
    {
        return sum[i].c >= re[0] && sum[i].r >= re[1] && sum[i].h >= re[2];
    }

    vector<int> getTriggerTime(vector<vector<int>> &increase, vector<vector<int>> &requirements)
    {
        // 前缀和
        for (int i = 0; i < increase.size(); i++)
        {
            sum[i + 1].c = sum[i].c + increase[i][0];
            sum[i + 1].r = sum[i].r + increase[i][1];
            sum[i + 1].h = sum[i].h + increase[i][2];
        }

        // 二分答案
        int len = requirements.size();
        int days = increase.size();
        vector<int> res(len);

        for (int i = 0; i < len; i++)
        {
            int l = 0, r = days + 1;
            while (l < r)
            {
                int mid = (l + r) >> 1;
                if (check(mid, requirements[i]))
                    r = mid;
                else
                    l = mid + 1;
            }

            res[i] = (l > days) ? -1 : l;
        }

        return res;
    }
};

int main()
{
    int n, m;
    cin >> n;

    vector<vector<int>> incs, reqs;
    while (n--)
    {
        vector<int> crh(3);
        cin >> crh[0] >> crh[1] >> crh[2];
        incs.push_back(crh);
    }

    cin >> m;
    while (m--)
    {
        vector<int> crh(3);
        cin >> crh[0] >> crh[1] >> crh[2];
        reqs.push_back(crh);
    }

    Solution s;

    for (auto a : s.getTriggerTime(incs, reqs))
    {
        cout << a << " ";
    }
    cout << endl;
    return 0;
}

/*
3
2 8 4
2 5 0
10 9 8
4
2 11 3
15 10 7
9 17 12
8 1 14



4
0 4 5
4 8 8 
8 6 1
10 10 0
5
12 11 6
20 2 6
9 2 6
10 18 3
8 14 9
*/